require.config({
    baseUrl: 'js/common',
    paths: {
        "deptData": "../data/deptData",
        "pagin": "./pagin"
    }
});
define(['deptData', 'pagin'], function(Depts, pagin) {
    /*
     *------助管信息管理
     *增加助管：addZhuguan
     *删除助管：deleteZG
     *查询数据：queryData
     *向服务器获取所有数据：getAllData
     *向服务器查询数据（页）：getPage
     *获取学院信息列表，获取银行列表：getDept,getBanks，目前是写死的
     *-------说明
     *-------
     *-------
     */
    var dept_info = new Vue({
        el: '#dept-data',
        data: {
            /*查询对象*/
            query: {
                "rows": 10,
                "page": 1,
                "deptId": ''
            },
            /*pageNum 总页数*/
            pageNum: 1,
            /*当前页码*/
            curPage: 1,
            /*pages用于构造page*/
            pages: [],
            /*部门列表*/
            depts: [],
            deptItem: [],
            select_deptNo: '',
            /*选中删除的助管对象*/
            deleteItem: '',
            /*选中的要修改的学院信息*/
            editItem: '',
            /*要添加的助管对象*/
            addItem: ''
        },
        methods: {
            /*初始化*/
            init: function() {
                var _self = this;
                _self.queryData();
                _self.queryDepts();
            },
            /*查询数据*/
            queryDepts: function() {
                var _self = this;
                var query = {
                    "rows": 100,
                    "page": 1,
                    "deptId": ''
                };
                // _self.select_deptId = "";
                _self.deptItem = [];
                Depts.queryDepts(query).done(function(data) {
                    $.each(data.result, function(i, value) {
                        _self.deptItem.push(value);
                    });
                })
            },
            queryData: function(str) {
                var _self = this;
                _self.query.deptId = _self.select_deptId;
                _self.depts = [];
                if (str == 'page1') {
                    _self.query.page = 1;
                    _self.query.deptId = '';
                    _self.curPage = 1;
                }
                if (str == 'all') {
                    _self.query.page = 1;
                    // _self.query.deptId='';
                    _self.curPage = 1;
                }
                // _self.deptItem = [];
                Depts.queryDepts(_self.query).done(
                    function(data) {
                        $.each(data.result, function(i, value) {
                            _self.depts.push(value);
                        });
                        /* 设置分页*/
                        if (data.total > _self.query.rows) {
                            /*如果总数大于10就分页*/
                            /*_self.totalPage 总页数*/
                            var totalPage = Math.ceil(data.total / _self.query.rows);

                            var page_result = pagin.paginGetli(totalPage, _self.curPage);
                            _self.pages = page_result.pages;
                            console.log(_self.pages);
                        } else {
                            _self.pages = [];
                        }
                    }
                );
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                _self.query.page = cur_page;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理分页查询*/
            submitPage: function(p, b) {
                var _self = this;
                if (b) {
                    return false;
                }
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            /*添加一条学院信息*/
            addDept: function() {
                var _self = this;
                // var additem = JSON.stringify(_self.addItem);
                Depts.addDepts(_self.addItem).done(
                    function(data) {
                        alert(data.message);
                        setTimeout(function() {
                            $('#myModal-add').modal('hide');
                            // _self.queryData('page1');
                            location.reload()
                        // _self.select_deptId = "";
                        // _self.queryDepts();

                        }, 1000);
                    }
                );
            },
            /*提交删除一条助管信息到modal*/
            deleteInfo: function(i, name) {
                var _self = this;
                _self.deleteItem = {
                    "deptId": i,
                    "deptName": name
                };
            },
            /*提交删除一条助管信息到服务器*/
            deleteDept: function() {
                var _self = this;
                var deleteItem = JSON.stringify({
                    "deptId": _self.deleteItem.deptId
                });
                Depts.delDepts(_self.deleteItem).done(
                    function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-delete').modal('hide');
                            // _self.queryData('page1');
                            // _self.queryDepts();
                            location.reload()
                        }, 1000);
                    }
                );
            },
            /*提交编辑一条助管信息到modal*/
            editInfo: function(i, name) {
                console.log('editInfo');
                var _self = this;
                _self.editItem = {
                    "deptId": i,
                    "deptName": name
                };
            },
            /*提交编辑到服务器*/
            editDept: function() {
                var _self = this;
                var editItem = JSON.stringify({
                    "deptId": _self.editItem.deptId,
                    "deptName": _self.editItem.deptName,
                    "jobNum": parseInt(_self.editItem.jobNum)
                });
                Depts.updateDepts(_self.editItem).done(
                    function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-edit').modal('hide');
                            _self.queryData();
                            _self.queryDepts();

                        }, 1000);
                    }
                );
            }
        },
        watch: {
            select_deptId: function(val) {
                console.log(val);
                this.queryData('all');

            }
        }
    });
    dept_info.init();
});